<template>
  <div class="xtx-city" ref="target">
    <div class="select">
      <span class="placeholder" @click="toggle">{{ cityStr }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="isShow">
      <div v-if="loading" class="loading"></div>
      <template v-else>
        <span
          class="ellipsis"
          v-for="i in city"
          @click="cityClick(i)"
          :key="i.code"
          >{{ i.name }}</span
        >
      </template>
    </div>
  </div>
</template>
<script>
/*
地址组件：
1：定义api获取数据并渲染
2：地址显示切换
3:点击非城市组件区域关闭城市列表
   a:全局的点击事件
    判断点击的dom不在城市组件内就关闭
3：默认数据设置显示（父组件可以控制这个显示）
4：选择地址功能，选择完成要更改地址
  a:点击后省变成市
*/
import { getCityInfo } from '@/api/product'
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { onClickOutside } from '@vueuse/core'
// onClickOutside(dom,()=>{如果点击在dom外就执行})
export default {
  name: 'XtxCity',
  props: {
    cityStr: {
      type: String,
      default: '请选择配送地址'
    }
  },
  setup (props, { emit }) {
    const isShow = ref(false)
    const loading = ref(false)
    const city = ref([])
    const initCity = ref([]) // 完整的城市数据备份
    // 打开城市列表
    function open () {
      isShow.value = true
      if (city.value.length) {
        return
      }
      loading.value = true
      getCityInfo().then(res => {
        // console.log(res)
        // setTimeout(() => {
        city.value = res
        initCity.value = res
        loading.value = false
        // }, 5000)
      })
    }
    function close () {
      isShow.value = false
      // 每次关闭都还原城市数据
      city.value = initCity.value
      // 清空cityParams
      for (const key in cityParams) {
        cityParams[key] = ''
      }
    }
    function toggle () {
      isShow.value ? close() : open()
    }
    // 点击非城市组件区域关闭列表(原生做法)
    const target = ref(null)
    // onMounted(() => {
    //   document.addEventListener('click', documentClickEvent)
    // })
    // onBeforeUnmount(() => {
    //   document.removeEventListener('click', documentClickEvent)
    // })
    // function documentClickEvent (e) {
    //   if (!target.value.contains(e.target)) {
    //     close()
    //   }
    // }
    // 点击非城市区域关闭城市列表
    onClickOutside(target, () => {
      close()
    })
    // 城市点击事件
    const cityParams = {
      provinceName: '',
      provinceCode: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      cityStr: ''
    }
    // 城市选择点击
    function cityClick (item) {
      // 省的选择
      if (item.level === 0) {
        cityParams.provinceName = item.name
        cityParams.provinceCode = item.code
        city.value = item.areaList
        // 市的选择
      } else if (item.level === 1) {
        cityParams.cityName = item.name
        cityParams.cityCode = item.code
        city.value = item.areaList
      } else {
        //   区的选择
        cityParams.countyName = item.name
        cityParams.countyCode = item.code
        cityParams.cityStr =
          cityParams.provinceName +
          ' ' +
          cityParams.cityName +
          ' ' +
          cityParams.countyName
        emit('change', cityParams)
        close()
      }

      //   if (item.level < 2) {
      //     city.value = item.areaList
      //   } else {
      //     close()
      //   }
    }

    return { city, isShow, toggle, loading, target, cityClick }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
